package com.yb.core.model;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import com.yb.core.enums.HttpCode;
import com.yb.core.exception.XczxBusinessException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/7 14:50
 * FileName: XczxResult
 * Description:
 */

@Data
@NoArgsConstructor
@AllArgsConstructor
@ApiModel
public class XczxResult<T> {

    @ApiModelProperty("响应码")
    private Integer code;
    @ApiModelProperty("响应文字")
    private String msg;

    @ApiModelProperty("响应数据")
    private T data;

    public static XczxResult success(){
        return new XczxResult(HttpCode.Response.SUCCESS.getCode(), HttpCode.Response.SUCCESS.getMsg(),null);
    }

    public static <R> XczxResult  success(R data){
        return new XczxResult(HttpCode.Response.SUCCESS.getCode(), HttpCode.Response.SUCCESS.getMsg(),data);
    }

    public static XczxResult error(){
        return new XczxResult(HttpCode.Response.ERROR.getCode(), HttpCode.Response.ERROR.getMsg(),null);
    }

    public static <R>  XczxResult error(R data){
        return new XczxResult(HttpCode.Response.ERROR.getCode(), HttpCode.Response.ERROR.getMsg(),data);
    }

    public static  XczxResult error(Integer code,String msg){
        return new XczxResult(code,msg,null);
    }
    public static  <R> XczxResult error(Integer code,String msg ,R data){
        return new XczxResult(code,msg,data);
    }

    public static  <R> XczxResult error(HttpCode.Response response){
        return new XczxResult(response.getCode(), response.getMsg(), null);
    }

    public static  <R> XczxResult error(HttpCode.Response response,R data){
        return new XczxResult(response.getCode(), response.getMsg(), data);
    }

    public static XczxResult clientError(){
        return new XczxResult(HttpCode.CLIENT_ERROR_CODE, HttpCode.Response.ERROR.getMsg(),null);
    }

    public static <R>  XczxResult clientError(R data){
        return new XczxResult(HttpCode.CLIENT_ERROR_CODE, HttpCode.Response.ERROR.getMsg(),data);
    }

    public static  XczxResult clientError(Integer code,String msg){
        return new XczxResult(code,msg,null);
    }
    public static  <R> XczxResult clientError(Integer code,String msg ,R data){
        return new XczxResult(code,msg,data);
    }

    public static  <R> XczxResult clientError(HttpCode.Response response){
        return new XczxResult(response.getCode(), response.getMsg(), null);
    }

    public static  <R> XczxResult clientError(HttpCode.Response response,R data){
        return new XczxResult(response.getCode(), response.getMsg(), data);
    }
    public static  XczxResult error(XczxBusinessException e){
        return new XczxResult(e.getCode(),e.getMessage(),null);
    }

    @JSONField(serialize = false)
    public String toJSON(){
        return JSONObject.toJSONString(this);
    }
}
